<template>
    <div>
        <div class="search-content">
            <el-form ref="searchForm" :inline="true" :model="searchContent" >
                <el-form-item label="合同编号：" prop="contract_id">
                    <el-input
                        class="search-content-input"
                        v-model="searchContent.contract_id"
                        placeholder="合同编号"
                    ></el-input>
                </el-form-item>
                <el-form-item label="申请店铺：" prop="store_name">
                    <el-input
                        class="search-content-input"
                        v-model="searchContent.store_name"
                        placeholder="申请店铺"
                    ></el-input>
                </el-form-item>
                <el-form-item label="申请时间：" prop="time">
                    <el-date-picker
                        v-model="searchContent.time"
                        type="daterange"
                        range-separator="至"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
                        value-format="timestamp"
                    ></el-date-picker>
                    <!-- value-format="yyyy-MM-dd" -->
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="search">搜索</el-button>
                    <el-button type="primary" @click="resetSearchData">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
        <div class="search-list">
            <el-table :data="goodsList" border :height="oHeight" :fit="true">
                <!-- <el-table-column align="center" type="selection" width="55"></el-table-column> -->
                <el-table-column type="index" width="50"></el-table-column>
                <el-table-column align="center" prop="contract_id" label="合同编号" min-width="100"></el-table-column>
                <el-table-column
                    align="center"
                    prop="ApplyTime"
                    label="申请时间"
                    min-width="160"
                    #default="{row}"
                >{{row.application_time*1000|dateFormatter('YYYY-MM-DD hh:mm:ss')}}</el-table-column>
                <el-table-column
                    align="center"
                    prop="ApplyTime"
                    label="生效时间"
                    min-width="160"
                    #default="{row}"
                >{{row.validate_time*1000|dateFormatter('YYYY-MM-DD hh:mm:ss')}}</el-table-column>
                <el-table-column align="center" prop="store_name" label="申请店铺" min-width="150"></el-table-column>
                <el-table-column align="center" prop="contact" label="联系人" min-width="100"></el-table-column>
                <el-table-column align="center" prop="contact_tel" label="联系方式" min-width="120"></el-table-column>
                <el-table-column align="center" prop="fee" label="费用(元)" min-width="70"></el-table-column>
                <el-table-column
                    align="center"
                    prop="status"
                    label="状态"
                    min-width="70"
                    #default="{row}"
                >
                    {{row.status==="0"?"未通过":""}}
                    {{row.status==="1"?"通过":""}}
                    {{row.status==="2"?"待审核":""}}
                    {{row.status==="3"?"已关闭":""}}
                </el-table-column>
                <el-table-column
                    align="center"
                    prop="adjunct"
                    label="附件下载"
                    min-width="70"
                    #default="{row}"
                >
                    <a
                        style="color:rgb(87, 144, 187);"
                        :href="imgHttp+'/upload/DMPFile/'+row.adjunct"
                        v-if="row.adjunct"
                    >下载</a>
                </el-table-column>
                <el-table-column
                    align="center"
                    prop="remark"
                    label="备注"
                    min-width="100"
                    #default="{row}"
                >
                    <el-popover placement="right" width="400" trigger="click">
                        <div class="remarkBox" v-html="row.remark?row.remark:'暂无备注!'"></div>
                        <el-button type="primary" slot="reference">查看</el-button>
                    </el-popover>
                </el-table-column>
                <el-table-column
                    fixed="right"
                    align="center"
                    prop="isInitiative"
                    label="操作"
                    min-width="250"
                    #default="{row}"
                >
                    <div style="color: #5790bb">
                        <div v-if="row.status==='3'">
                            <span @click="updateAudit(row,'1')">通过</span>
                            <el-divider direction="vertical"></el-divider>
                            <span @click="updateAudit(row,'3')">关闭</span>
                        </div>
                        <div v-if="row.status==='1'">
                            <span @click="changeStatus(row,'Open')">合同导入</span>
                            <el-divider direction="vertical"></el-divider>
                            <span @click="changeStatus(row,'Close')">店铺导入</span>
                        </div>
                    </div>
                </el-table-column>
            </el-table>
            <div class="bottom">
                <div class="btn-group">
                    <el-button type="primary" class="btn-bottom" @click="showContent('add')">新增</el-button>
                    <!-- <el-button type="primary" class="btn-bottom" @click="expertData">导出检索数据</el-button> -->
                </div>
                <!--分页部分-->
                <div class="paginationOne">
                    <el-pagination
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page="searchContent.currentPage"
                        :page-sizes="[10, 20, 50, 100]"
                        :page-size="searchContent.pageSize"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="total*1"
                    ></el-pagination>
                </div>
            </div>
        </div>
        <edit-drawer ref="editDrawer"></edit-drawer>
    </div>
</template>
<script>
import { resize } from "@/common/js/height.js";
import editDrawer from './show-edit-drawer';
import { dateFormatter, objScreen } from "@/common/js/util";
export default {
    components: { editDrawer },
    data() {
        return {
            goodsList: [],
            oHeight: 800,
            searchContent: {
                currentPage: 1,
                pageSize: localStorage.getItem('pageSize') * 1 || 10,
                contract_id: '',
                store_name: '',
                time: ''
            },
            searchObj: {},
            total: 0,
            imgHttp: ''
        };
    },
    created() {
        if (this.$store.state.isServer) {
            this.imgHttp = window.location.origin.replace('admin', 'img');
        } else {
            this.imgHttp = 'http://img.chejj.cn/'
        }

        this.getListData();
    },
    methods: {
        updateAudit(row, status) {
            this.$http.post("/admin/dataService/index.php?act=merchantSet&op=updateContractStatus", {
                id: row.id,
                status
            }).then(res => {
                if (res.code === 1) {
                    row.status = status;
                    this.$message.success(res.msg);
                } else if (res) {
                    this.$message.error(res.msg);
                }
            }).catch(err => {
                this.$message.error(err);
            })
        },
        showContent(type, row) {
            if (type === 'add') {
                this.$refs.editDrawer.show_dialog_fn('add', row)
            } else {
                this.$refs.editDrawer.show_dialog_fn('edit', row)
            }

        },
        changeStatus(row, status) {
            this.$http.post("/admin/dataService/index.php?act=merchantSet&op=updateContractStatus", {
                ID: row.ID,
                status
            }).then(res => {
                if (res.code === 1) {
                    row.ContractStatus = status;
                    this.$message.success(res.msg);
                } else if (res) {
                    this.$message.error(res.msg);
                }
            }).catch(err => {
                this.$message.error(err);
            })
        },
        getListData() {
            var data = this.searchObj;
            data.currentPage = this.searchContent.currentPage;
            data.eachNum = this.searchContent.pageSize;
            this.$http.post('/admin/dataService/index.php?act=intelligentClue&op=applyList', data).then((res) => {
                if (res.code === 1) {
                    this.total = res.data.total;
                    this.goodsList = res.data.list;
                    // this._message('success', res.msg);
                    // this.getListData();
                } else {
                    this.total = 0;
                    this.goodsList = [];
                    this._message('error', res.msg);
                }
            }).catch(err => {
                this._message('error', err);
            })
        },
        search() {
            this.searchContent.currentPage = 1;
            let { contract_id,
                store_name,
                time } = this.searchContent;
            this.searchObj = objScreen({
                startTime: time ? parseInt(time[0] / 1000) : '',
                endTime: time ? parseInt(time[1] / 1000) : '',
                contract_id,
                store_name
            })
            this.getListData();
        },
        resetSearchData() {
            this.$refs['searchForm'].resetFields();
        },
        handleSizeChange(pageSize) {
            localStorage.setItem('pageSize', pageSize);
            this.searchContent.pageSize = pageSize;
            this.searchContent.currentPage = 1;
            this.getListData();
        },
        handleCurrentChange(page) {
            this.searchContent.currentPage = page;
            this.getListData();
        },
    },
    mounted() {
        resize(this)
    },
};
</script>
<style lang="scss" scoped>
.bottom {
    display: flex;
    justify-content: space-between;
    align-items: center;
}
.search-content-input {
    width: 200px;
}
.remarkBox /deep/img {
    max-width: 800px;
}
</style>